package chapter06;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
    public FirstServlet() {
        //无参构造方法
        super();//调用父类的无参构造方法
    }

    @Override//重载修饰符 覆盖父类方法 用于销毁对象
    public void destroy() {//销毁方法
        super.destroy();//调用父类的destroy方法
    }

    @Override//重载修饰符 覆盖父类方法 用于初始化对象
    public void init() throws ServletException {
        //抛出ServletException异常
        super.init();//调用父类的init方法
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//处理GET请求
        response.setCharacterEncoding("UTF-8");//设置编码格式
        response.setContentType("text/html");//设置响应类型
        PrintWriter out = response.getWriter();//获取PrintWriter输出流
        //返回一个PrinWriter对象,Servlet输使用它来输出字符串形式的正文数据
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<html>");
        out.println("<head><TITLE>动态生成HTML文档</TITLE></head>");
        out.println("<body>");
        out.println("<table border='0'align='center'");
        out.println("<tr><td bgcolor='skyblue'colspan='2'>动态生成HTML文档</td></tr>");
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
        out.flush();//刷新输出流
        out.close();//关闭输出流
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//处理POST请求
        doGet(request, response);//调用doGet方法处理请求
    }
}